<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        var arr = [132, 22, 54, [12, 2, [12, 343]],
                [1212]
            ]
            // 数组扁平化
        function flatten1(arr) {
            var _arr = arr || [],
                fArr = [],
                len = _arr.length,
                item;
            for (var i = 0; i < len; i++) {
                item = _arr[i];
                if (_isArr(item)) {
                    fArr = fArr.concat(flatten1(item))
                } else {
                    fArr.push(item);
                }
            }
            return fArr

            function _isArr(obj) {
                return {}.toString.call(obj) === '[object Array]'
            }
        }
        console.log(flatten1(arr));
        Array.prototype.flatten2 = function() {
            var _arr = this,
                toStr = {}.toString;
            if (toStr.call(_arr) !== '[object Array]') {
                throw new Error('不是数组类型')
            }
            var fArr = [];
            _arr.forEach(function(elem) {
                toStr.call(elem) === '[object Array]' ?
                    fArr = fArr.concat(elem.flatten2()) :
                    fArr.push(elem)
            })
            return fArr
        }
        console.log(arr.flatten2());

        Array.prototype.flatten3 = function() {
            var _arr = this,
                toStr = {}.toString;
            if (toStr.call(_arr) !== '[object Array]') {
                throw new Error('不是数组类型')
            }

            return _arr.reduce(function(prev, elem) {
                return prev.concat(
                    toStr.call(elem) === '[object Array]' ? elem.flatten3() : elem
                )
            }, [])
        }
        console.log(arr.flatten3());

        const flatten4 = arr => arr.reduce((prev, elem) => prev.concat({}.toString.call(elem) === '[object Array]' ? flatten4(elem) : elem), [])
        console.log(flatten4(arr));
    </script>
</body>

</html>